import {
    reqLogin, reqDoctorInfo, reqAdminInfo, reqUserPag, reqUserAdminMenu, reqUploadAdminAvatar, reqUploadAdminInfo,
    reqUserRoleAndAll, reqSaveUserRole, reqUserInfo, reqUploadDoctorAvatar, reqUploadDoctorInfo
} from "@/api"
import { removeItem, setItem } from "@/utils/auth"
// state：仓库存储数据的地方
const state = {
    // 当前登录的用户信息
    userInfo: {},
    // 当前登录的token
    token: "",
    // 当前登录的用户身份
    role: [],
    // 用户列表
    userList: [],
    // 用户后台菜单
    userAdminMenu: [],

}
// mutations：修改state的唯一手段，因此第一个参数必须是state，第二个参数为将要修改的数据
const mutations = {
    setUserInfo(state, res) {
        state.userInfo = res
    },
    setToken(state, token) {
        state.token = token
    },
    setRole(state, role) {
        state.role = role
    },
    setUserList(state, res) {
        state.userList = res
    },
    setUserAdminMenu(state, res) {
        state.userAdminMenu = res
    },
    setUserAvatar(state, res) {
        state.userInfo.avatar = res
    }
}
// action：书写业务逻辑（method方法）
const actions = {
    // 获取用户信息
    async getUserInfo({ commit }, data) {
        let res = null
        // 不同身份来发送请求
        if (data == 1) {
            res = await reqUserInfo()
            // 请求成功
            if (res.success) {
                commit('setUserInfo', res.data.userDto)
                commit('setMenuInfo', res.data.adminMenuList)
                commit('setRole', res.data.roleList)
            }
        }
        else if (data == 2) {
            res = await reqDoctorInfo()
            // 请求成功
            if (res.success) {
                commit('setUserInfo', res.data.doctorDto)
                commit('setMenuInfo', res.data.adminMenuList)
                commit('setRole', res.data.roleList)
            }
        }
        else if (data == 3) {
            res = await reqAdminInfo()
            // 请求成功
            if (res.success) {
                commit('setUserInfo', res.data.adminDto)
                commit('setMenuInfo', res.data.adminMenuList)
                commit('setRole', res.data.roleList)
            }
        }
        else if (data == 4) {
            res = await reqAdminInfo()
            // 请求成功
            if (res.success) {
                commit('setUserInfo', res.data.adminDto)
                commit('setMenuInfo', res.data.adminMenuList)
                commit('setRole', res.data.roleList)
            }
        }
        return res
    },
    // 登录
    async getLogin({ commit }, data) {
        let res = await reqLogin(data)
        if (res.success) {
            // 保存token
            setItem("token", res.data)
            // 保存身份
            setItem("identify", data.identify)
            // 保存token
            commit('setToken', res.data)
        }
        return res
    },
    // 退出登录
    async logout({ commit }) {
        removeItem("token")
        removeItem("identify")
    },
    // 获取用户分页
    async getUserPag({ commit }) {
        let res = await reqUserPag()
        if (res.success) commit('setUserList', res.data.userList)
        return res
    },
    // 获取用户后台菜单
    async getUserAdminMenu({ commit }, data) {
        let res = await reqUserAdminMenu(data)
        if (res.success) commit("setUserAdminMenu", res.data)
        return res
    },
    // 获取用户角色以及全部角色
    async getUserRoleAndAll({ commit }, data) {
        return await reqUserRoleAndAll(data)
    },
    // 保存分配角色
    async saveUserRole({ commit }, data) {
        return await reqSaveUserRole(data)
    },
    // 上传医生头像
    async uploadDoctorAvatar({ commit }, data) {
        return await reqUploadDoctorAvatar(data)
    },
    // 更新用户信息
    async uploadDoctorInfo({ commit }, data) {
        let res = await reqUploadDoctorInfo(data)
        if (res.success) commit("setUserInfo", res.data)
        return res
    },
    // 上传管理员头像
    async uploadAdminAvatar({ commit }, data) {
        return await reqUploadAdminAvatar(data)
    },
    // 更新管理员信息
    async uploadAdminInfo({ commit }, data) {
        let res = await reqUploadAdminInfo(data)
        if (res.success) commit("setUserInfo", res.data)
        return res
    },

}
// getters：计算属性
const getters = {
    username: state => {
        return state.userInfo.username
    },
}

export default {
    state,
    mutations,
    actions,
    getters
}